package org.example.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtil {

    public static final String KEY = "HJYNB";
    public static String getToken(Map<String, Object> claims){
        //生成jwt
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))//设置过期时间
                .sign(Algorithm.HMAC256(KEY));//配置密钥
    }

    public static Map<String, Object> parseToken(String token){
        //验证token
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims").asMap();

    }
}
